package com.zzzy.cont.controller.mycon;


import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zzzy.common.util.FileUtil;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.cont.service.mycon.MyConSignService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

@Tag(name = "我的合同-签订中", description = "con112")
@Validated
@RestController
@RequestMapping("/con/my")
public class MyConSignController {

    @Resource
    private MyConSignService myConSignService;

    @PutMapping("/applySign")
    @Operation(summary = "申请签订")
    @Parameter(name = "id", description = "合同ID", required = true)
    @Parameter(name = "userId", description = "用户ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:my:update')")
    public AjaxResult applySign(@NotNull(message = "合同ID不能为空") Long id, @NotNull(message = "用户ID不能为空") Long userId) {
        myConSignService.applySign(id, userId);
        return AjaxResult.success();
    }

    @PostMapping(value = "/updateSignFile/{id}")
    @Operation(summary = "保存签订中文件")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:my:update')")
    public void updateSignFile(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id , HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        byte[] byteArray = FileUtil.toByteArray(fs.getFileStream());
        fs.setCustomSaveResult("ok");
        myConSignService.updatePdf(id, byteArray);
        fs.close();
    }

    @PutMapping("/reAudit/{id}")
    @Operation(summary = "重新审核")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:my:update')")
    public AjaxResult reAudit(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        myConSignService.reAudit(id);
        return AjaxResult.success();
    }

    @PostMapping("/uploadSignFile")
    @Operation(summary = "上传对方盖章合同")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:my:add')")
    public AjaxResult uploadSignFile(@Parameter(name = "conFile", description = "合同文件", required = true)
                                         @NotNull(message = "合同文件不能为空") MultipartFile conFile, @NotNull(message = "合同ID不能为空") Long id) {
        myConSignService.uploadSignFile(id, conFile);
        return AjaxResult.success();
    }

    @PutMapping("/signComplete/{id}")
    @Operation(summary = "签订完毕进入履行中")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:my:update')")
    public AjaxResult signComplete(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        myConSignService.signComplete(id);
        return AjaxResult.success();
    }

}
